<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_Model_Award extends Core_Model_Item_Abstract {
	// Properties
	

	protected $_searchTriggers = array ('search', 'title', 'description' );
	
	/**
	 * Gets an absolute URL to the page to view this item
	 *
	 * @return string
	 */
	public function getHref($params = array()) {
		$slug = $this->getSlug ();
		
		$params = array_merge ( array ('route' => 'badge_award_profile', 'reset' => true, 'award_id' => $this->award_id, 'slug' => $slug ), $params );
		$route = $params ['route'];
		$reset = $params ['reset'];
		unset ( $params ['route'] );
		unset ( $params ['reset'] );
		return Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( $params, $route, $reset );
	}
	
	public function getBadge() {
		return Engine_Api::_ ()->badge ()->getBadge ( $this->badge_id );
	}
	
	public function getUser() {
		return Engine_Api::_ ()->user ()->getUser ( $this->user_id );
	}
	
	// Interfaces
	/**
	 * Gets a proxy object for the comment handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function comments() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'comments', 'core' ) );
	}
	
	/**
	 * Gets a proxy object for the like handler
	 *
	 * @return Engine_ProxyObject
	 **/
	public function likes() {
		return new Engine_ProxyObject ( $this, Engine_Api::_ ()->getDbtable ( 'likes', 'core' ) );
	}
	
	public function getPhotoUrl($type = null) {
		$badge = $this->getBadge ();
		if ($badge instanceof Badge_Model_Badge) {
			return $this->getBadge ()->getPhotoUrl ( $type );
		}
		return null;
	}
	
	protected function _delete() {
		if ($this->_disableHooks)
			return;
		
		// Delete all comments
		$comments = $this->comments ()->getAllComments ();
		foreach ( $comments as $comment ) {
			$comment->delete ();
		}
		
		// Delete all likes
		$likes = $this->likes ()->getAllLikes ();
		foreach ( $likes as $like ) {
			$like->delete ();
		}
		
		parent::_delete ();
	}

}